﻿Pui.newSH = {
	init: function() {
		_user = {
			uid: 0,
			nick: '',
			spacename: '',
			newmsg: 0
		};
		_is_login = false;
		var n = Pui.cookie('LoginInfo');
		if (n) {
			var arr = n.split('$***$');
			_user = {
				uid: arr[0].replace('LoginInfo=', ''),
				nick: arr[1],
				spacename: arr[2],
				newmsg: 0,
				spaceurl: arr[3]
			};
			_is_login = true;
			this.islogin();
		} else {
			this.unlogin();
		};
		this.doSearch();
		this.router();
	}, 
	router:function(){
			var pageType = "";
			var n = null;
  		var bodyID = document.body.id.toLowerCase();
  		var hostName = window.location.hostname;
  		if ( bodyID != "" )
  		{	
  			
  			if( bodyID == "b-index" )
  			{
  					pageType = "index";
  					n = Pui.cookie('router');
  			}
  			else
  			{
  				if ( bodyID.lastIndexOf('-') > 1  && bodyID.indexOf('-index') > 2)
					{
						pageType = bodyID.substr(bodyID.indexOf('b-')+2,bodyID.indexOf('-index')-2);
						if(pageType=="car")
						{
			   			pageType="auto";
						}
						n = Pui.cookie('router');
					}
				}
  		}
  		
  		if(document.location.hash == "#test")
			{ 	
					alert("bodyID="+bodyID+",pageType="+pageType+"cookie="+n);
			}
  		
			if (n != null) {
				 if( n == "router=true" ) 
				 { 
				  	if( pageType == "index" ){
				  		 window.location = "http://pomoho.gogochannel.com/" ; 
				  	}
				  	else
				  	{
				  		 window.location = "http://pomoho.gogochannel.com/"+pageType ; 
				  	}
				 }
			}
	},
	checkrouter:function(){
			var pageType = "";
  		var bodyID = document.body.id.toLowerCase();
  		var hostName = window.location.hostname;
  		if ( bodyID != "" )
  		{	
  			if( bodyID == "b-index" )
  			{
  					pageType = "index";
  					Pui.loadScript("http://www.pomoho.com/do/interfaces/router.aspx?pagetype="+pageType+""); 
						this.router();
  			}
  			else
  			{
  				if ( bodyID.lastIndexOf('-') > 1  && bodyID.indexOf('-index') > 2)
					{
						pageType = bodyID.substr(bodyID.indexOf('b-')+2,bodyID.indexOf('-index')-2);
						if(pageType=="car")
						{
			   			pageType="auto";
						}
						Pui.loadScript("http://www.pomoho.com/do/interfaces/router.aspx?pagetype="+pageType+""); 
						this.router();
					}
				}
			}
	},
	unlogin: function() {
		var str = '';
		str += '<li class="s"><a target="_blank" class="f" title="返回首页" href="http://www.pomoho.com/">首页</a></li>';
		str += '<li class="s"><a class="f" title="登录" href="javascript:Pui.newSH.loginfo()">登录</a></li>';
		str += '<li class="s logout"><a class="f" title="注册" href="http://www.pomoho.com/ui/login.aspx" target="_blank">注册</a></li>';
		$("#TopNav").html(str);
	},
	islogin: function() {
		var t_time = this.localTime();
		var str = '';
		str += '<li class="s"><a target="_blank" title="返回首页" href="http://www.pomoho.com/">首页</a></li>';
		str += '<li class="s">' + t_time + ',<a class="user" target="_blank" href="http://home.pomoho.com/' + _user.spaceurl + '.shtml" >' + _user.nick + '</a></li>';
		str += '<li class="s"><a class="count" title="你的帐户" href="http://www.pomoho.com/manage/index.aspx" target="_blank" >帐户&nbsp;&nbsp;&nbsp;&nbsp;</a>';
		str += '   <div class="d s3">';
		str += '     <ul>';
		str += '       <li><a class="d_item" title="我的视频" href="http://www.pomoho.com/manage/managevideo.aspx" target="_blank" >我的视频</a></li>';
		str += '       <li><a class="d_item" title="我的订阅" href="http://www.pomoho.com/manage/subscribevblog.aspx" target="_blank" >我的订阅</a></li>';
		str += '       <li><a class="d_item" title="我的收藏" href="http://www.pomoho.com/manage/managevideo.aspx?type=favs" target="_blank" >我的收藏</a></li>';
		str += '       <li><a class="d_item" title="我的相册" href="http://www.pomoho.com/manage/managealbum.aspx" target="_blank" >我的相册</a></li>';
		str += '     </ul>';
		str += '     <div class="m1"><a title="更多管理" href="http://www.pomoho.com/manage/index.aspx" target="_blank" >更多..&nbsp;&nbsp;</a></div>';
		str += '   </div>';
		str += ' </li>';
		str += ' <li class="s"><a title="你的消息" href="http://www.pomoho.com/manage/messagelist.aspx?type=1" target="_blank" >消息</a><span>(0)</span>';
		str += '   <div class="d s2">';
		str += '     <div class="title"><div class="d_video">主题</div><div class="d_author">消息来源</div></div>';
		str += '     <ul>';
		str += '     </ul>';
		str += '     <div class="m"><a title="全部消息" href="http://www.pomoho.com/manage/messagelist.aspx?type=1" target="_blank" >全部消息&gt;&gt;</a></div>';
		str += '   </div>';
		str += ' </li>';
		str += ' <li class="logout"><a title="退出登录" href="http://www.pomoho.com/ui/logout.aspx?AfterLoginReturnURL=' + window.location + '" >退出</a></li>';
		$("#TopNav").html(str);
		this.SH();
		Pui.loadScript("http://notice.pomoho.com/loadmessage2.0.aspx");
	},
	SH: function() {
		var tim;
		$("#TopNav .s").hover(function() {
			$(this).siblings().find(".d").hide();
			$(this).find(".d").show();
			clearTimeout(tim);
		},
		function() {
			tim = setTimeout(overDiv, 500);
			function overDiv() {
				$("#TopNav").find(".d").slideUp();
			}
		});
	},
	localTime: function() {
		var time = new Date();
		var hour = time.getHours();
		var t_time = ($.browser.msie) ? String(time).split(" ")[3].substr(0, 5) : String(time).split(" ")[4].substr(0, 5);
		switch (hour) {
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
			t_time = '早上好';
			break;
		case 8:
		case 9:
		case 10:
		case 11:
			t_time = '上午好';
			break;
		case 12:
			t_time = '中午好';
			break;
		case 13:
		case 14:
		case 15:
		case 16:
		case 17:
			t_time = '下午好';
			break;
		case 18:
		case 19:
		case 20:
		case 21:
		case 22:
		case 23:
			t_time = '晚上好';
			break;
		}
		return t_time;
	},
	Msginit: function(jsonMsg) {
		var str = '';
		var len = jsonMsg.length;
		if (len <= 1) {
			var str = '<li style="width:100%">您还没有消息</li>';
		} else {
			for (var i = 1; i <= len - 1; i++) {
				var msgtypename;
				switch (jsonMsg[i].msgtype) {
				case 1:
					msgtypename = "站内消息"
					break;
				case 2:
					msgtypename = "系统消息"
					break
				case 3:
					msgtypename = "公告"
					break
				}
				str += '<li id="msg' + i + '">';
				if (jsonMsg[i].msgtype != 3) {
					str += '<div class="d_video"><a title="' + jsonMsg[i].msgtitle + '" href="http://www.pomoho.com/manage/readmessage.aspx?receiveid=' + jsonMsg[i].msgid + '&rettype=1" target="_blank" >' + jsonMsg[i].msgtitle + '</a></div>';
				} else {
					str += '<div class="d_video"><a title="' + jsonMsg[i].msgtitle + '" href="http://www.pomoho.com/manage/readmessage.aspx?bulletinid=' + jsonMsg[i].msgid + '&rettype=1" target="_blank" >' + jsonMsg[i].msgtitle + '</a></div>';
				}
				str += '<div class="d_author">' + msgtypename + '</div>';
				str += '<a class="d_close" title="删除消息" href="javascript:Pui.newSH.Msgdel(' + jsonMsg[i].msgid + ',' + jsonMsg[i].msgtype + ',' + i + ')" ></a> </li>';
			}
		}
		$("#TopNav .s2 ul").html(str);
		$("#TopNav .s2").parent().find("span").html("(" + jsonMsg[0] + ")");
	},
	Msgdel: function(msgid, msgtype, num) {
		$("#msg" + num).remove();
		var Msgurl = "http://www.pomoho.com/do/interfaces/delmessage.aspx?messageID=" + msgid + "&Userid=" + _user.uid + "&messageType=" + msgtype;
		Pui.loadScript(Msgurl);
		var num = parseInt($("#TopNav .s2").parent().find("span").html().substr(1)) - 1;
		if (num <= 0) num = 0;
		$("#TopNav .s2").parent().find("span").html("(" + num + ")");
	},
	doSearch: function(e) {
		if (typeof(s) != 'undefined') return
		else s = 1;
		$("#goSearch").click(function() {
			var values = $.trim($("#searchGame").val());
			if (!values) {
				alert("关键字不能为空");
				$("#searchGame").focus();
			} else {
				var param = encodeURIComponent(values.replace(/[\/_]/g, ' '));
				var url = "http://search.pomoho.com/videosearch.aspx?_keyword=" + param;
				window.open(url, '_blank');
			}
		});
	},
	logDiv: function() {
		var str = '';
		if ($.browser.msie) str += '<iframe id="zhezhao"></iframe>';
		else str += '<div id="zhezhao"></div>';
		str += ' <div id="Prompt" class="prompt">';
		str += '    <div class="k log_v">';
		str += '      <div class="meat">';
		str += '        <div class="close"><img alt="关闭" src="http://static01.pomoho.com/themes/site/default/images/games/close.gif" width="16" height="16" /></div>';
		str += '        <h3></h3>';
		str += '        <p class="p2">您的帐号：';
		str += '          <input id="uidName" type="text" onkeydown="Pui.newSH.submitF(event)" />';
		str += '        </p>';
		str += '        <p class="p2">登录密码：';
		str += '          <input id="uidPwd" type="password" onkeydown="Pui.newSH.submitF(event)" />';
		str += '          <a href="http://www.pomoho.com/ui/findpassword.aspx" target="_blank">忘记密码</a></p>';
		str += '        <p class="pbt">';
		str += '          <input id="btnlog" onclick="return Pui.newSH.login()" class="log" type="button" value="登录" />';
		str += '          <input id="reg" class="log" type="button" value="注册" />';
		str += '        </p>';
		str += '      </div>';
		str += '    </div>';
		str += '  </div>';
		$("body").prepend(str);
		if ($.browser.msie) {
			var cw = window.frames['zhezhao'];
			cw.document.open();
			cw.document.write('<style type="text/css">body{background:#000;}</style>');
			cw.document.close();
		}
		$("#Prompt .close img").bind("click", Pui.newSH.closeDiv);
		$("#reg").click(function() {
			window.open('http://www.pomoho.com/ui/login.aspx');
		});
	},
	showLogDiv: function() {
		if (!$("#Prompt").length) {
			Pui.newSH.logDiv();
		}
		var doc = document;
		var docElement = doc.documentElement;
		var sHeight = docElement.clientHeight;
		var dH = docElement.scrollHeight > sHeight ? docElement.scrollHeight: sHeight;
		var scH = docElement.scrollTop >= 0 ? (docElement.scrollTop + sHeight / 2) : sHeight;
		var h = scH - $("#Prompt").height() / 2;
		var w = docElement.clientWidth / 2 - $("#Prompt").width() / 2;
		$("#zhezhao").height(dH).show();
		$("#Prompt").css({
			top: h,
			left: w
		});
	},
	loginfo: function() {
		Pui.newSH.showLogDiv();
		$("#Prompt").show();
		$("#Prompt .k")[0].className = "k log_v";
		$('#uidName').focus();
		if (document.attachEvent) {
			document.attachEvent('onkeyup', Pui.newSH.EXC);
		} else {
			document.addEventListener('keyup', Pui.newSH.EXC, false)
		}
	},
	submitF: function(e) {
		var event = window.event || e;
		if (event.keyCode == 13) $("#btnlog").click();
	},
	EXC: function(e) {
		var event = window.event || e;
		if (event.keyCode == 27) Pui.newSH.closeDiv();
	},
	login: function() {
		var userName = $.trim($("#uidName").val());
		var pwd = $("#uidPwd").val();
		if (userName == '' || pwd == '') {
			alert("用户名或者密码不能为空");
			return false
		} else {
			Pui.newSH.logAjax(userName, pwd);
		}
	},
	closeDiv: function() {
		$("#Prompt").hide();
		$("#zhezhao").hide();
	},
	logAjax: function(userName, pwd) {
		$.ajax({
			url: "http://" + window.location.hostname + "/do/interfaces/sendajaxreq.aspx",
			dataType: "xmlDocument",
			type: "post",
			data: "action=http://www.pomoho.com/ui/logindo.aspx&form_name=" + userName + "&form_password=" + pwd + "&type=gamelogin&random=" + Math.random(),
			error: function() {
				alert('可能网络问题,登录失败,请重试');
			},
			success: function(result) {
				var userlog = eval(result);
				switch (parseInt(userlog[0].stat)) {
				case - 1 : alert("登录帐号不存在");
					break;
				case - 2 : alert("用户名或者密码错误,请重试");
					break;
				case 1:
					Pui.newSH.cookie('UserInfo', userlog[1].UserInfo, {
						path: '/',
						domain: '.pomoho.com',
						expires: 1 / 6
					});
					Pui.newSH.cookie('LoginInfo', userlog[2].LoginInfo, {
						path: '/',
						domain: '.pomoho.com',
						expires: 1 / 6
					});
					var urlString = window.location.toString().toLowerCase();
					if (urlString.indexOf("pomoho.com/combo") != -1 && (typeof(flag) == 'undefined' || flag == null)) {
						window.location.reload();
						return;
					}
					if( urlString.indexOf("pomoho.com/gameunion") != -1 )
					{
						window.location.reload();
						return;
					}
					Pui.newSH.init();
					if (typeof(flag) == 'undefined' || flag == null) Pui.newSH.closeDiv();
					else if (flag == 1) {
						Pui.user.haveFriend();
						flag = null;
					} else if (flag == 2) {
						Pui.loadScript("http://www.pomoho.com/do/video/dealajaxdata.aspx?itype=2&imdbid=" + flvid);
						flag = null;
						Pui.newSH.closeDiv();
					} else if (flag == 3) {
						f_addfriend(_userid);
						flag = null;
						Pui.newSH.closeDiv();
					} else if (flag == 4) {
						flag = null;
						Pui.newSH.closeDiv();
						f_comboInterFace(addPram.otype, addPram.atype, addPram.comboid, _user.uid);
						Pui.newSH.closeDiv();
					} else if (flag == 5) {
						flag = null;
						Pui.ReUrl(pathUrl);
						Pui.newSH.closeDiv();
					}else if(flag==6){//登陆后添加留言或评论
						flag=null;
						var content = ($('body').attr('id')!='SpaceColumn'?$('#GuestBook textarea'):$('#Comment textarea')).val().replace("<", "&lt;").replace(">", "&gt;").replace("&", "<--@@-@@-->");
						scontent = escape(content);
						var sValidCode = document.getElementById("tb_validcode").value;
						$('body').attr('id')!='SpaceColumn'?Pui.loadScript(baseurl+"/space/personjs.aspx?dotype=4&userid="+_space_user.uid+"&content="+escape(content)+ "&validcode=" + sValidCode, {random:true}):Pui.loadScript("http://www.pomoho.com/do/video/dealajaxdata.aspx?itype=1&imdbid=" + flvid + "&content=" + scontent + "&validcode=" + sValidCode, {random:true});
						setTimeout("Pui.video.commentReplyFill()", 1000);
						GetValidCodeImg();
						Pui.newSH.closeDiv();
					}
					break;
				default:
					alert("系统错误,请重试，错误代码：" + userlog[1].ErrMsg);
					Pui.newSH.closeDiv();
				}
			}
		});
	},
	cookie: function(name, value, options) {
		if (typeof(value) != 'undefined') {
			options = options || {};
			if (value === null) {
				value = '';
				options.expires = -1;
			}
			var expires = '';
			if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
				var date;
				if (typeof options.expires == 'number') {
					date = new Date();
					date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
				} else {
					date = options.expires;
				}
				expires = '; expires=' + date.toUTCString();
			}
			var path = options.path ? '; path=' + options.path: '';
			var domain = options.domain ? '; domain=' + options.domain: '';
			var secure = options.secure ? '; secure': '';
			document.cookie = [name, '=', name + '=' + value, expires, path, domain, secure].join('');
		} else {
			var cookieValue = null;
			if (document.cookie && document.cookie != '') {
				var cookies = document.cookie.split(';');
				for (var i = 0; i < cookies.length; i++) {
					var cookie = jQuery.trim(cookies[i]);
					if (cookie.substring(0, name.length + 1) == (name + '=')) {
						cookieValue = unescape(cookie.substring(name.length + 1));
						break;
					}
				}
			}
			return cookieValue;
		}
	}
}; 